<?php
class LinkAction extends BaseAction{
	/**
	 * Show all links
	 * Enter description here ...
	 */
	public function index(){
		$Link = M('Link');
		$links = $Link->order('link_sort')->select();
		$this->assign('links',$links);
		$this->display();
	}
	/**
	 * Add a new link!
	 */
	public function addurl(){
		$Link = M('Link');
		if (!empty($_FILES)) {
			//如果有文件上传 上传附件
			$nlink['link_logo']=$this->_upload();
			//$this->forward();
		}
		$nlink['link_name']=$_POST['link_name'];
		$nlink['link_text']=$_POST['link_text'];
		$nlink['link_url']=$_POST['link_url'];
		$nlink['link_sort']=$_POST['link_sort'];
		$nlink['link_state']=1;
		$Link->add($nlink);
		$this->redirect('index', '',3,'A new Link is created!');
	}
	/**
	 * update the url
	 */
	public function updateurl(){
		$Link = M('Link');
		if (!empty($_FILES)) {
			//如果有文件上传 上传附件
			$nlink['link_logo']=$this->_upload();
			//$this->forward();
		}
		$nlink['link_id'] = $_POST['link_id'];
		$nlink['link_name']=$_POST['link_name'];
		$nlink['link_text']=$_POST['link_text'];
		$nlink['link_url']=$_POST['link_url'];
		$nlink['link_sort']=$_POST['link_sort'];
		$nlink['link_state']=1;
		$Link->save($nlink);
		$this->redirect('index', '',3,'Update succeed!');
	}
	/**
	 * Edit link html
	 */
	public function editurl(){
		$Link = M('Link');
		$alink = $Link->where('link_id='.$_GET['id'])->find();
		$this->assign('alink',$alink);
		$this->display();
	}
	/**
	 * Delete a url
	 */
	public function deleteurl(){
		$Link = M('Link');
		$alink = $Link->where('link_id='.$_GET['id'])->delete();
		$this->redirect('index', '',4,'Delete a url');
	}
	/**
	 * Upload file
	 * Enter description here ...
	 */
	protected function _upload() {
		import("@.ORG.UploadFile");
		$upload = new UploadFile();
		//设置上传文件大小
		$upload->maxSize = 3292200;
		//设置上传文件类型
		$upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
		//设置附件上传目录
		$upload->savePath = '__APP__/../Public/Uploads/';
		//设置需要生成缩略图，仅对图像文件有效
		$upload->thumb = true;
		// 设置引用图片类库包路径
		$upload->imageClassPath = '@.ORG.Image';
		//设置需要生成缩略图的文件后缀
		$upload->thumbPrefix = 'm_,s_';  //生产2张缩略图
		//设置缩略图最大宽度
		$upload->thumbMaxWidth = '100,100';
		//设置缩略图最大高度
		$upload->thumbMaxHeight = '100,100';
		//设置上传文件规则
		$upload->saveRule = uniqid;
		//删除原图
		$upload->thumbRemoveOrigin = true;
		if (!$upload->upload()) {
			//捕获上传异常
			//$this->error($upload->getErrorMsg());
			return '';
		} else {
			//取得成功上传的文件信息
			$uploadList = $upload->getUploadFileInfo();
			import("@.ORG.Image");
			//给m_缩略图添加水印, Image::water('原文件名','水印图片地址')
			Image::water($uploadList[0]['savepath'].'m_'.$uploadList[0]['savename'], '__APP__/../Public/Images/logo2.png');
			$_POST['image'] = $uploadList[0]['savename'];
		}
		$model = M('Photo');
		//保存当前数据对象
		$data['image'] = $_POST['image'];
		return $data['image'];
	}
}
?>